package com.sgroup.studycountry.infrastructure.cofig.cors;

/**
 * @author umi
 * @date 2021/10/27 20:02
 */

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author czx
 * @date 2021/8/4 10:05
 */
public class CorsFilter implements Filter {

    /**
     * 只对options请求进行处理还是会有跨域问题
     * 所以暂时对所有的请求进行处理。
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) response;
        //解决跨域的问题
        res.setHeader("Access-Control-Allow-Origin", ((HttpServletRequest) request).getHeader("Origin"));
        res.setHeader("Access-Control-Allow-Credentials", "true");
        res.setHeader("Access-Control-Allow-Headers", "*");
        res.setHeader("Access-Control-Expose-Headers", "*");
        res.setHeader("Access-Control-Allow-Methods", "*");
        res.setHeader("Access-Control-Max-Age", "18000L");
        chain.doFilter(request, response);
    }
}
